package com.zj.leetcode.easy.listNode;

/**
 * @program: algorithm
 * @description: 合并两个有序链表
 * 21 merge-two-sorted-lists
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class MergeTwoSortedLists {
    public static void main(String[] args) {
        Solution solution = new MergeTwoSortedLists().new Solution();
//        ListNode l1 = new ListNode(1,
//                new ListNode(2,
//                        new ListNode(4, null)));
//        ListNode l2 = new ListNode(1,
//                new ListNode(3,
//                        new ListNode(4, null)));

//        ListNode l1 = new ListNode(2, null);
//        ListNode l2 = new ListNode(1, null);

//        ListNode l1 = new ListNode(1, null);
//        ListNode l2 = new ListNode(2, null);


//        ListNode l1 = new ListNode(1,
//                new ListNode(2,
//                        new ListNode(4, null)));
//        ListNode l2 = new ListNode(1,
//                new ListNode(3,
//                        new ListNode(4, new ListNode(4, new ListNode(5, null)))));

        ListNode l1 = new ListNode(5, null);

        ListNode l2 = new ListNode(1,
                new ListNode(2,
                        new ListNode(4, null)));
        System.out.println(solution.mergeTwoLists(l1, l2));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    class Solution {
        public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

            if (list1 == null) {
                return list2;
            }
            if (list2 == null) {
                return list1;
            }

            ListNode head = null;
            ListNode cur = new ListNode(-1);
            if (list1.val <= list2.val) {
                ListNode node = new ListNode(list1.val, null);
                head = node;
                cur.next = node;
                cur = cur.next;
                list1 = list1.next;
            } else {
                ListNode node = new ListNode(list2.val, null);
                head = node;
                cur.next = node;
                cur = cur.next;
                list2 = list2.next;
            }
            while (list1 != null && list2 != null) {
                while (list1 != null && list1.val <= list2.val) {
                    cur.next = new ListNode(list1.val, null);
                    cur = cur.next;
                    list1 = list1.next;
                }

                while (list1 != null && list2 != null && list2.val<list1.val){
                    cur.next = new ListNode(list2.val, null);
                    cur = cur.next;
                    list2 = list2.next;
                }

            }

            while (list1 != null) {
                cur.next = new ListNode(list1.val, null);
                cur = cur.next;
                list1 = list1.next;
            }

            while (list2 != null) {
                cur.next = new ListNode(list2.val, null);
                cur = cur.next;
                list2 = list2.next;
            }
            return head;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
